/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.NewUserController;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import model.Base;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ira
 */
public class NewUserWindow extends JDialog {

//<editor-fold defaultstate="collapsed" desc="nom des composants">
	public static final String TXT_NAME = "txt_name";
	public static final String TXT_FIRSTNAME = "txt_firstname";
	public static final String TXT_PSEUDO = "txt_pseudo";
	public static final String TXT_PASS = "txt_pass";
	public static final String TXT_PASS_VERIF = "txt_pass_verif";
	public static final String BTN_CREATE = "btn_create";
	public static final String BTN_CANCEL = "btn_cancel";
	public static final String CBB_TYPE = "cbb_type";
//</editor-fold>
	private JLabel lblNameError = new JLabel("");
	private JLabel lblFirstNameError = new JLabel("");
	private JLabel lblPseudoError = new JLabel("");
	private JLabel lblPassError = new JLabel("");
	private JLabel lblPassVerifError = new JLabel("");
	private JLabel lblName = new JLabel("Nom");
	private JLabel lblFirstName = new JLabel("Prénom");
	private JLabel lblPseudo = new JLabel("Identifiant");
	private JLabel lblPass = new JLabel("Mot de passe");
	private JLabel lblType = new JLabel("Type");
	private JLabel lblInfo = new JLabel("Création d'un nouvel utilisateur");
	private JLabel lblInfoCreation = new JLabel("");
	private JTextField txtName = new JTextField(9);
	private JTextField txtFirstName = new JTextField(9);
	private JTextField txtPseudo = new JTextField(9);
	private JTextField txtPass = new JPasswordField(9);
	private JTextField txtPassVerif = new JPasswordField(9);
	private JComboBox cbbType;
	private JButton btnCancel = new JButton("Annuler");
	private JButton btnCreate = new JButton("Créer");
	private NewUserController controller;

	public NewUserWindow(Base model) {
		this.controller = new NewUserController(model, this);
		this.setLayout(new MigLayout());

		this.cbbType = new JComboBox(model.getUserTypes().toArray());
		this.txtFirstName.setName(NewUserWindow.TXT_FIRSTNAME);
		this.txtName.setName(NewUserWindow.TXT_NAME);
		this.txtPass.setName(NewUserWindow.TXT_PASS);
		this.txtPassVerif.setName(NewUserWindow.TXT_PASS_VERIF);
		this.txtPseudo.setName(NewUserWindow.TXT_PSEUDO);
		this.btnCancel.setName(NewUserWindow.BTN_CANCEL);
		this.btnCreate.setName(NewUserWindow.BTN_CREATE);
		this.cbbType.setName(NewUserWindow.CBB_TYPE);

// Listeners
		this.btnCancel.addActionListener(this.controller);
		this.btnCreate.addActionListener(this.controller);
		this.txtName.addFocusListener(this.controller);
		this.txtFirstName.addFocusListener(this.controller);
		this.txtPseudo.addFocusListener(this.controller);
		this.txtPass.addFocusListener(this.controller);
		this.txtPassVerif.addFocusListener(this.controller);

		this.add(this.lblInfo, "span");
		this.add(this.lblName);
		this.add(this.txtName);
		this.add(this.lblNameError, "wrap");
		this.add(this.lblFirstName);
		this.add(this.txtFirstName);
		this.add(this.lblFirstNameError, "wrap");
		this.add(this.lblPseudo);
		this.add(this.txtPseudo);
		this.add(this.lblPseudoError, "wrap");
		this.add(this.lblPass);
		this.add(this.txtPass);
		this.add(this.lblPassError, "wrap");
		this.add(this.txtPassVerif, "cell 1 5");
		this.add(this.lblPassVerifError, "wrap");
		this.add(this.lblType);
		this.add(this.cbbType, "wrap");
		this.add(this.btnCancel);
		this.add(this.btnCreate, "growx");
		this.add(this.lblInfoCreation);

		this.setTitle("Ajout d'un utilisateur");

		this.setSize(350, 245); // 217 245
		this.setModal(true);
		this.setVisible(true);
	}

// Getters / Setters
	public String getUserPass() {
		return this.txtPass.getText();
	}

	public String getUserPassVerif() {
		return this.txtPassVerif.getText();
	}

	public String getUserName() {
		return this.txtName.getText();
	}

	public String getUserFirstName() {
		return this.txtFirstName.getText();
	}

	public String getUserPseudo() {
		return this.txtPseudo.getText();
	}

	public String getUserType() {
		return (String) this.cbbType.getSelectedItem();
	}

	public void setNameInfo(String txt, boolean error) {
		this.lblNameError.setText(txt);
		if(error) {
			this.lblNameError.setForeground(Color.red);
		}
		else {
			this.lblNameError.setForeground(Color.decode("#0C8010"));
		}
	}

	public void setFirstNameInfo(String txt, boolean error) {
		this.lblFirstNameError.setText(txt);
		if(error) {
			this.lblFirstNameError.setForeground(Color.red);
		}
		else {
			this.lblFirstNameError.setForeground(Color.decode("#0C8010"));
		}
	}

	public void setPseudoInfo(String txt, boolean error) {
		this.lblPseudoError.setText(txt);
		if(error) {
			this.lblPseudoError.setForeground(Color.red);
		}
		else {
			this.lblPseudoError.setForeground(Color.decode("#0C8010"));
		}
	}

	public void setPassInfo(String txt, boolean error) {
		this.lblPassError.setText(txt);
		if(error) {
			this.lblPassError.setForeground(Color.red);
		}
		else {
			this.lblPassError.setForeground(Color.decode("#0C8010"));
		}
	}

	public void setPassVerifInfo(String txt, boolean error) {
		this.lblPassVerifError.setText(txt);
		if(error) {
			this.lblPassVerifError.setForeground(Color.red);
		}
		else {
			this.lblPassVerifError.setForeground(Color.decode("#0C8010"));
		}
	}
	
	public void confirmUserCreation(int errors) {
		if(errors == 0) {
			// User créé
			this.lblInfoCreation.setForeground(Color.decode("#0C8010"));
			this.lblInfoCreation.setText("Utilisateur créé");
			this.txtName.setText("");
			this.txtFirstName.setText("");
			this.txtPass.setText("");
			this.txtPassVerif.setText("");
			this.txtPseudo.setText("");
			this.controller.verifData();
		}
		else {
			// User non créé
			this.lblInfoCreation.setForeground(Color.red);
			this.lblInfoCreation.setText("Erreurs: non créé");
		}
	}
}